package com.netx.ebs;
import javax.servlet.http.*;



class Cookies {
	
	private static final int _CLIENT_ID_AGE = 365 * 24 * 60 * 60; // 1 year, in seconds

	public static Cookie createClientID() {
		SessionIdGenerator sig = new SessionIdGenerator();
		String sessionID = sig.nextStringIdentifier()+sig.nextStringIdentifier()+sig.nextStringIdentifier();
		return new Cookie(Constants.COOKIE_CLIENT_ID, sessionID);
	}

	public static Cookie createUserID(long userCode) {
		return new Cookie(Constants.COOKIE_USER_ID, userCode+"");
	}

	public static void deleteUserID(HttpServletResponse response) {
		Cookie c = new Cookie(Constants.COOKIE_USER_ID, "");
		c.setMaxAge(0);
		response.addCookie(c);
	}

	public static Cookie[] getCookies(EbsRequest request) {
		Cookie[] array = new Cookie[2];
		array[0] = request.getCookie(Constants.COOKIE_CLIENT_ID);
		array[1] = request.getCookie(Constants.COOKIE_USER_ID);
		return array;
	}

	public static void refreshCookies(Cookie[] array, User u, HttpServletResponse response) {
		array[0].setMaxAge(_CLIENT_ID_AGE);
		if(u.getSessionTimeoutTime() == 0) {
			// No timeout:
			array[1].setMaxAge(_CLIENT_ID_AGE);
		}
		else {
			array[1].setMaxAge(u.getSessionTimeoutTime() * 60);
		}
		response.addCookie(array[0]);
		response.addCookie(array[1]);
	}

}
